使用numpy进行数据分析

  1. 生成矩阵X1和X2,两者随机从[-10,10]内整数中抽取10个样本,且形状分别为2 $\times$ 5和5 $\times$ 2,随机种子分别为0和1,输出结果;

  2. 将X1和X2的转置用两种方法实现按行合并,分别赋值给Xnew1和Xnew2,输出结果,并比较结果的差异;

  3. 对矩阵X1实现按行求和和按列求和;

  4. 计算X1 $\times$ X2;

  5. 生成矩阵X,两者随机从[-10,10]内整数中抽取25个样本,形状为5 $\times$ 5,随机种子为0,同时生成向量y=[3,2,4,6,1],求解$X\beta=y$的方程解;

In [1]:
from IPython.core.interactiveshell import InteractiveShell 
InteractiveShell.ast_node_interactivity = "all" 

import numpy as np
In [2]:
#第一题
np.random.seed(0)
X1=np.random.randint(-10,10,(2,5))
np.random.seed(0)
X2=np.random.randint(-10,10,(5,2))
print(X1)
print(X2)
[[  2   5 -10  -7  -7]
 [ -3  -1   9   8  -6]]
[[  2   5]
 [-10  -7]
 [ -7  -3]
 [ -1   9]
 [  8  -6]]
In [3]:
#第二题
Xnew1=np.concatenate((X1,X2.T))
Xnew2=np.stack((X1,X2.T))
print(Xnew1.ndim,Xnew2.ndim)
print(Xnew1,"\n\n",Xnew2)
#发现输出后Xnew1是拼接而成的,输入2维数组返回2维数组;Xnew2是堆叠形成的,输入2维返回3维
2 3
[[  2   5 -10  -7  -7]
 [ -3  -1   9   8  -6]
 [  2 -10  -7  -1   8]
 [  5  -7  -3   9  -6]] 

 [[[  2   5 -10  -7  -7]
  [ -3  -1   9   8  -6]]

 [[  2 -10  -7  -1   8]
  [  5  -7  -3   9  -6]]]
In [4]:
#第三题
print("Row sums:",X1.sum(axis=1))   #按行求和
print("Column sums:",X1.sum(axis=0))   #按列求和
Row sums: [-17   7]
Column sums: [ -1   4  -1   1 -13]
In [5]:
#第四题
print("X1*X2为\n",np.matmul(X1,X2))
print("X1*X2为\n",X1@X2)    #另一种方式
X1*X2为
 [[ -25  -16]
 [-115   73]]
X1*X2为
 [[ -25  -16]
 [-115   73]]
In [6]:
#第五题
np.random.seed(0)
X=np.random.randint(-10,10,(5,5))
y=np.array([3,2,4,6,1])
np.linalg.solve(X,y)
Out[6]:
array([ 0.17456845, -0.48801655, -0.96155309,  1.04056303, -0.39419373])

使用pandas进行数据分析

shops_nm.xlsx文档里面记录了有关火锅店铺的基本信息,包括点名,评分,菜品等。

  1. 把shops_nm.xlsx读入到数据框raw_data中, 输出其列名, 行数和列数大小, 前五行的内容;

  2. 挑选出数据框raw_data中评价数不缺失的行,挑选出的数据赋值给数据框raw_data2,输出行数, 列数以及前五行的内容;

  3. 对于数据框raw_data2,利用apply函数以及自定义函数clean_price(x)(函数作用为去除字符串x中的中文内容)去除"人均"列中的所有中文字符,并该列数据格式转化为浮点型数据,输出raw_data2前五行的内容;

  4. 对于数据框raw_data2,新建一列数据,列名为"商家等级", 若'评分'<=2,商家等级为0,若2<'评分'<=3.5,商家等级为1,其余'评分'的商家等级为2,输出raw_data2前五行的内容;

  5. 对于数据框raw_data2,根据'关键词'和'商家等级'分组计算平均评分,并输出结果;

In [7]:
from IPython.core.interactiveshell import InteractiveShell 
InteractiveShell.ast_node_interactivity = "all" 

import pandas as pd
In [8]:
#第一题
raw_data=pd.read_excel("shops_nm.xlsx")
raw_data.head(n=5)    #输出前五行
raw_data.shape        #行列数
print(raw_data.columns)        #列名
Out[8]:
店名 关键词 城市 评分 评价数 人均 地址 营业时间 菜名
0 老北京涮羊肉 火锅 xa 4.4 877.0 45.5 西安市雁塔区朱雀大街250号东方大酒店西门斜对面(子午路站下车向北走60米路西) 11:00-21:00 【羊肉】【豆腐】【麻酱】【精品肥牛】【粉丝】【羔羊肉】【牛肚】【油豆皮】【香菇】【豆皮】【土...
1 鲜上鲜文鱼庄(望庭国际店) 火锅 xa 4.6 535.0 56 西安市高新区高新路80号望庭国际一栋楼10106室 11:00-23:00 【菌菇大拼】【麻酱】【菠萝飞饼】【鸳鸯锅】【酸菜鸳鸯锅】【青菜】【毛肚】【文鱼】【菌汤】【鱼...
2 大龙燚火锅(李家村店) 火锅 xa 4.6 29.0 大概92左右 西安市碑林区雁塔北路时亿广场南座2楼李家村华润万家外 10:00-22:00 【手撕竹笋】【麻辣排骨】【砣砣牛肉】【菠萝飞饼】【鸭血】【四川金针菇】【麻辣牛肉】【贡菜】【...
3 鲜上鲜文鱼庄(阳阳国际店) 火锅 xa 4.6 906.0 56 西安市雁塔区朱雀大街132号阳阳国际广场C座2楼 11:00-22:00 【海带】【鸳鸯锅】【冻豆腐】【荷包豆腐】【生鱼片】【毛肚】【文鱼】【清汤锅】【山珍菌汤锅】【...
4 大龙燚火锅(粉巷店) 火锅 xa 4.6 2253.0 人均:100 西安市碑林区粉巷南院门15A南苑中央广场食尚南苑2F 周一至周日 10:00-21:00 【麻辣排骨】【千层毛肚】【鸳鸯锅】【鸭血】【天味香肠】【薄土豆】【功夫黄瓜】【清汤锅】【印度...
Out[8]:
(699, 9)
Index(['店名', '关键词', '城市', '评分', '评价数', '人均', '地址', '营业时间', '菜名'], dtype='object')
In [9]:
#第二题
raw_data2=raw_data[raw_data["评价数"].isnull()==False]
raw_data2.shape
raw_data2.head(n=5)
Out[9]:
(695, 9)
Out[9]:
店名 关键词 城市 评分 评价数 人均 地址 营业时间 菜名
0 老北京涮羊肉 火锅 xa 4.4 877.0 45.5 西安市雁塔区朱雀大街250号东方大酒店西门斜对面(子午路站下车向北走60米路西) 11:00-21:00 【羊肉】【豆腐】【麻酱】【精品肥牛】【粉丝】【羔羊肉】【牛肚】【油豆皮】【香菇】【豆皮】【土...
1 鲜上鲜文鱼庄(望庭国际店) 火锅 xa 4.6 535.0 56 西安市高新区高新路80号望庭国际一栋楼10106室 11:00-23:00 【菌菇大拼】【麻酱】【菠萝飞饼】【鸳鸯锅】【酸菜鸳鸯锅】【青菜】【毛肚】【文鱼】【菌汤】【鱼...
2 大龙燚火锅(李家村店) 火锅 xa 4.6 29.0 大概92左右 西安市碑林区雁塔北路时亿广场南座2楼李家村华润万家外 10:00-22:00 【手撕竹笋】【麻辣排骨】【砣砣牛肉】【菠萝飞饼】【鸭血】【四川金针菇】【麻辣牛肉】【贡菜】【...
3 鲜上鲜文鱼庄(阳阳国际店) 火锅 xa 4.6 906.0 56 西安市雁塔区朱雀大街132号阳阳国际广场C座2楼 11:00-22:00 【海带】【鸳鸯锅】【冻豆腐】【荷包豆腐】【生鱼片】【毛肚】【文鱼】【清汤锅】【山珍菌汤锅】【...
4 大龙燚火锅(粉巷店) 火锅 xa 4.6 2253.0 人均:100 西安市碑林区粉巷南院门15A南苑中央广场食尚南苑2F 周一至周日 10:00-21:00 【麻辣排骨】【千层毛肚】【鸳鸯锅】【鸭血】【天味香肠】【薄土豆】【功夫黄瓜】【清汤锅】【印度...
In [10]:
#第三题
import warnings
warnings.filterwarnings("ignore")

def clean_price(x):
    filter_words=['人均:',"人均","大概","左右","差不多"]
    if type(x) is int or type(x) is float:
        return x
    for word in filter_words:
        if word in x:
            x=x.replace(word,"")
    return x

raw_data2["人均"]=raw_data2["人均"].apply(clean_price)
raw_data2["人均"]=raw_data2["人均"].astype(float)
raw_data2.head(n=5)

# 改动了一下自定义函数
def clean_price(x):
    filter_words=['人均:',"人均","大概","左右","差不多"]
    if type(x) is int or type(x) is float:
        return float(x)
    for word in filter_words:
        if word in x:
            x=x.replace(word,"")
    return x

raw_data2["人均"]=raw_data2["人均"].apply(clean_price)
raw_data2.head(n=5)
Out[10]:
店名 关键词 城市 评分 评价数 人均 地址 营业时间 菜名
0 老北京涮羊肉 火锅 xa 4.4 877.0 45.5 西安市雁塔区朱雀大街250号东方大酒店西门斜对面(子午路站下车向北走60米路西) 11:00-21:00 【羊肉】【豆腐】【麻酱】【精品肥牛】【粉丝】【羔羊肉】【牛肚】【油豆皮】【香菇】【豆皮】【土...
1 鲜上鲜文鱼庄(望庭国际店) 火锅 xa 4.6 535.0 56.0 西安市高新区高新路80号望庭国际一栋楼10106室 11:00-23:00 【菌菇大拼】【麻酱】【菠萝飞饼】【鸳鸯锅】【酸菜鸳鸯锅】【青菜】【毛肚】【文鱼】【菌汤】【鱼...
2 大龙燚火锅(李家村店) 火锅 xa 4.6 29.0 92.0 西安市碑林区雁塔北路时亿广场南座2楼李家村华润万家外 10:00-22:00 【手撕竹笋】【麻辣排骨】【砣砣牛肉】【菠萝飞饼】【鸭血】【四川金针菇】【麻辣牛肉】【贡菜】【...
3 鲜上鲜文鱼庄(阳阳国际店) 火锅 xa 4.6 906.0 56.0 西安市雁塔区朱雀大街132号阳阳国际广场C座2楼 11:00-22:00 【海带】【鸳鸯锅】【冻豆腐】【荷包豆腐】【生鱼片】【毛肚】【文鱼】【清汤锅】【山珍菌汤锅】【...
4 大龙燚火锅(粉巷店) 火锅 xa 4.6 2253.0 100.0 西安市碑林区粉巷南院门15A南苑中央广场食尚南苑2F 周一至周日 10:00-21:00 【麻辣排骨】【千层毛肚】【鸳鸯锅】【鸭血】【天味香肠】【薄土豆】【功夫黄瓜】【清汤锅】【印度...
Out[10]:
店名 关键词 城市 评分 评价数 人均 地址 营业时间 菜名
0 老北京涮羊肉 火锅 xa 4.4 877.0 45.5 西安市雁塔区朱雀大街250号东方大酒店西门斜对面(子午路站下车向北走60米路西) 11:00-21:00 【羊肉】【豆腐】【麻酱】【精品肥牛】【粉丝】【羔羊肉】【牛肚】【油豆皮】【香菇】【豆皮】【土...
1 鲜上鲜文鱼庄(望庭国际店) 火锅 xa 4.6 535.0 56.0 西安市高新区高新路80号望庭国际一栋楼10106室 11:00-23:00 【菌菇大拼】【麻酱】【菠萝飞饼】【鸳鸯锅】【酸菜鸳鸯锅】【青菜】【毛肚】【文鱼】【菌汤】【鱼...
2 大龙燚火锅(李家村店) 火锅 xa 4.6 29.0 92.0 西安市碑林区雁塔北路时亿广场南座2楼李家村华润万家外 10:00-22:00 【手撕竹笋】【麻辣排骨】【砣砣牛肉】【菠萝飞饼】【鸭血】【四川金针菇】【麻辣牛肉】【贡菜】【...
3 鲜上鲜文鱼庄(阳阳国际店) 火锅 xa 4.6 906.0 56.0 西安市雁塔区朱雀大街132号阳阳国际广场C座2楼 11:00-22:00 【海带】【鸳鸯锅】【冻豆腐】【荷包豆腐】【生鱼片】【毛肚】【文鱼】【清汤锅】【山珍菌汤锅】【...
4 大龙燚火锅(粉巷店) 火锅 xa 4.6 2253.0 100.0 西安市碑林区粉巷南院门15A南苑中央广场食尚南苑2F 周一至周日 10:00-21:00 【麻辣排骨】【千层毛肚】【鸳鸯锅】【鸭血】【天味香肠】【薄土豆】【功夫黄瓜】【清汤锅】【印度...
In [11]:
#第四题
import warnings
warnings.filterwarnings("ignore")

def cate_shops(x):
    if x <= 2: return 0
    elif x <=3.5: return 1
    else: return 2
raw_data2['商家等级'] = raw_data2['评分'].apply(cate_shops)
raw_data2.head()
Out[11]:
店名 关键词 城市 评分 评价数 人均 地址 营业时间 菜名 商家等级
0 老北京涮羊肉 火锅 xa 4.4 877.0 45.5 西安市雁塔区朱雀大街250号东方大酒店西门斜对面(子午路站下车向北走60米路西) 11:00-21:00 【羊肉】【豆腐】【麻酱】【精品肥牛】【粉丝】【羔羊肉】【牛肚】【油豆皮】【香菇】【豆皮】【土... 2
1 鲜上鲜文鱼庄(望庭国际店) 火锅 xa 4.6 535.0 56.0 西安市高新区高新路80号望庭国际一栋楼10106室 11:00-23:00 【菌菇大拼】【麻酱】【菠萝飞饼】【鸳鸯锅】【酸菜鸳鸯锅】【青菜】【毛肚】【文鱼】【菌汤】【鱼... 2
2 大龙燚火锅(李家村店) 火锅 xa 4.6 29.0 92.0 西安市碑林区雁塔北路时亿广场南座2楼李家村华润万家外 10:00-22:00 【手撕竹笋】【麻辣排骨】【砣砣牛肉】【菠萝飞饼】【鸭血】【四川金针菇】【麻辣牛肉】【贡菜】【... 2
3 鲜上鲜文鱼庄(阳阳国际店) 火锅 xa 4.6 906.0 56.0 西安市雁塔区朱雀大街132号阳阳国际广场C座2楼 11:00-22:00 【海带】【鸳鸯锅】【冻豆腐】【荷包豆腐】【生鱼片】【毛肚】【文鱼】【清汤锅】【山珍菌汤锅】【... 2
4 大龙燚火锅(粉巷店) 火锅 xa 4.6 2253.0 100.0 西安市碑林区粉巷南院门15A南苑中央广场食尚南苑2F 周一至周日 10:00-21:00 【麻辣排骨】【千层毛肚】【鸳鸯锅】【鸭血】【天味香肠】【薄土豆】【功夫黄瓜】【清汤锅】【印度... 2
In [12]:
#第五题
raw_data2.groupby(by=['关键词','商家等级']).agg({'评分':'mean'})
Out[12]:
评分
关键词 商家等级
火锅 0 0.046875
1 3.161538
2 4.562857
烧烤 0 0.000000
1 3.080000
2 4.564545

使用Matplotlib绘图

  1. 将merge_shop_coupon_nm.xls读入到数据框merge_data中, 输出其列名, 行数和列数大小, 前五行的内容;

  2. 设置全局中文字体为'SimHei';

  3. 设置画布大小为(15,10),绘制如下四幅子图,第一幅子图展示人均评价关系图,第二幅子图展示人均评价关系局部图,评价数截取500-6500之间,人均截取20-60,第三幅子图按关键词分组绘制人均评价关系局部图,截取范围与第二幅子图类似,第四幅绘制购买人数评价数目关系图,购买人数和评价数绘图范围都截取0-3000;

  4. 设置画布大小为(10,5),绘制如下纵向排列和横向排列的直方图;

  5. 设置画布大小为(10,8),绘制如下反映购买人数评价数关系的箱型图

In [13]:
from IPython.core.interactiveshell import InteractiveShell 
InteractiveShell.ast_node_interactivity = "all" 

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt # 导入Matplotlib中的画图函数pyplot,命名为plt
In [14]:
#第一题
merge_data=pd.read_excel("merge_shop_coupon_nm.xls")
merge_data.head(n=5)    #输出前五行
merge_data.shape        #行列数
print(merge_data.columns)        #列名
Out[14]:
Unnamed: 0 店名 关键词 城市 评分 评价数 人均 地址 营业时间 菜名 团购价 购买人数
0 0 老北京涮羊肉 火锅 xa 4.4 877 45.5 西安市雁塔区朱雀大街250号东方大酒店西门斜对面(子午路站下车向北走60米路西) 11:00-21:00 【羊肉】【豆腐】【麻酱】【精品肥牛】【粉丝】【羔羊肉】【牛肚】【油豆皮】【香菇】【豆皮】【土... 118.5 1692
1 1 大龙燚火锅(粉巷店) 火锅 xa 4.6 2253 100.0 西安市碑林区粉巷南院门15A南苑中央广场食尚南苑2F 周一至周日 10:00-21:00 【麻辣排骨】【千层毛肚】【鸳鸯锅】【鸭血】【天味香肠】【薄土豆】【功夫黄瓜】【清汤锅】【印度... 88.0 19584
2 2 鲜上鲜文鱼庄(凤城五路店) 火锅 xa 4.5 1398 56.0 西安市未央区凤城五路地铁D口出人人乐5楼 全天 【生菜】【鸳鸯锅】【千叶豆腐】【荷包豆腐】【生鱼片】【毛肚】【文鱼】【鱼丸】【撒尿牛丸】【山... 52.0 11798
3 3 蜜悦士鲜牛肉时尚火锅(凯德广场店) 火锅 xa 4.4 48 63.0 西安市雁塔区南二环凯德广场四楼东南角 10:00-21:00 【吊龙伴】【三花腱】【番茄锅】【招牌牛舌】【油豆皮】【油炸豆腐皮】【菌汤鸳鸯锅】【手工面】【... 59.9 40
4 4 大自在火锅(和平村店) 火锅 xa 4.3 214 84.0 西安市莲湖区三桥和平村十字西南角伯乐城市广场6层 暂无时间 【蔬菜拼盘】【黄豆芽】【虾饺】【撒尿牛肉丸】【油条】【毛肚】【自在小酥肉】【鸡爪】【自在嫩牛... 59.5 1282
Out[14]:
(197, 12)
Index(['Unnamed: 0', '店名', '关键词', '城市', '评分', '评价数', '人均', '地址', '营业时间', '菜名',
       '团购价', '购买人数'],
      dtype='object')
In [15]:
#第二题
import matplotlib
matplotlib.rcParams['font.sans-serif']=['SimHei']
matplotlib.rcParams['axes.unicode_minus']=False
In [16]:
#第三题
import warnings
warnings.filterwarnings("ignore")
import matplotlib
matplotlib.rcParams['font.sans-serif']=['SimHei']
matplotlib.rcParams['axes.unicode_minus']=False

fig=plt.figure(figsize=(15,10))
 # 创建子图
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,3)
ax4 = fig.add_subplot(2,2,4)

ax1.set_title("人均评价关系图")
ax1.set_xlabel("人均")
ax1.set_ylabel("评价数")
ax1.scatter(x=merge_data["人均"], y=merge_data["评价数"], color="r", marker='v')

ax2.set_title("人均评价关系局部图")
ax2.set_xlabel("人均")
ax2.set_ylabel("评价数")
ax2.scatter(x=merge_data["人均"], y=merge_data["评价数"], color="r", marker='v')
ax2.set_xlim(20,60)
ax2.set_ylim(500,6500)
ax2.hlines(2000, 20, 60, colors="r", linestyle="dashed")

ax3.set_title("人均评价关系局部图")
ax3.set_xlabel("人均")
ax3.set_ylabel("评价数")
ax3.scatter(x=merge_data["人均"][merge_data['关键词']=='火锅'], y=merge_data["评价数"][merge_data['关键词']=='火锅'], color="r", marker='v',label = '火锅')
ax3.scatter(x=merge_data["人均"][merge_data['关键词']!='火锅'], y=merge_data["评价数"][merge_data['关键词']!='火锅'], color="b", marker='o',label='非火锅')
ax3.legend(loc='upper left',bbox_to_anchor=(0,1))
ax3.set_xlim(20,60)
ax3.set_ylim(500,6500)
ax3.hlines(2000, 20, 60, colors="r", linestyle="dashed")

ax4.set_title("人数评价数目关系图")
ax4.set_xlabel("购买人数")
ax4.set_ylabel("评价数")
ax4.scatter(x=merge_data["购买人数"], y=merge_data["评价数"], color="r", marker='v')
ax4.plot(np.arange(3000), np.arange(3000), color='blue')
ax4.set_xlim(0,3000)
ax4.set_ylim(0,3000)

plt.show()
Out[16]:
Text(0.5, 1.0, '人均评价关系图')
Out[16]:
Text(0.5, 0, '人均')
Out[16]:
Text(0, 0.5, '评价数')
Out[16]:
<matplotlib.collections.PathCollection at 0x232ac225df0>
Out[16]:
Text(0.5, 1.0, '人均评价关系局部图')
Out[16]:
Text(0.5, 0, '人均')
Out[16]:
Text(0, 0.5, '评价数')
Out[16]:
<matplotlib.collections.PathCollection at 0x232ac24d160>
Out[16]:
(20.0, 60.0)
Out[16]:
(500.0, 6500.0)
Out[16]:
<matplotlib.collections.LineCollection at 0x232ac24d730>
Out[16]:
Text(0.5, 1.0, '人均评价关系局部图')
Out[16]:
Text(0.5, 0, '人均')
Out[16]:
Text(0, 0.5, '评价数')
Out[16]:
<matplotlib.collections.PathCollection at 0x232ac225d90>
Out[16]:
<matplotlib.collections.PathCollection at 0x232ac24dcd0>
Out[16]:
<matplotlib.legend.Legend at 0x232ac24dca0>
Out[16]:
(20.0, 60.0)
Out[16]:
(500.0, 6500.0)
Out[16]:
<matplotlib.collections.LineCollection at 0x232ac273a30>
Out[16]:
Text(0.5, 1.0, '人数评价数目关系图')
Out[16]:
Text(0.5, 0, '购买人数')
Out[16]:
Text(0, 0.5, '评价数')
Out[16]:
<matplotlib.collections.PathCollection at 0x232ac24dc40>
Out[16]:
[<matplotlib.lines.Line2D at 0x232ac26c100>]
Out[16]:
(0.0, 3000.0)
Out[16]:
(0.0, 3000.0)
In [17]:
#第四题
fig=plt.figure(figsize=(10,5))
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)
ax1.hist(merge_data['评分'],bins=30,color='r',rwidth=0.9)
ax2.hist(merge_data["评分"], bins=30, color="b", rwidth=0.9,cumulative = 'True', orientation = 'horizontal')
ax1.set_xlabel("评分")
plt.show()
Out[17]:
(array([ 1.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,
         1.,  0.,  0.,  0.,  0.,  3.,  1.,  2.,  1.,  1.,  0.,  8., 20.,
        26., 62., 47., 23.]),
 array([0.        , 0.16666667, 0.33333333, 0.5       , 0.66666667,
        0.83333333, 1.        , 1.16666667, 1.33333333, 1.5       ,
        1.66666667, 1.83333333, 2.        , 2.16666667, 2.33333333,
        2.5       , 2.66666667, 2.83333333, 3.        , 3.16666667,
        3.33333333, 3.5       , 3.66666667, 3.83333333, 4.        ,
        4.16666667, 4.33333333, 4.5       , 4.66666667, 4.83333333,
        5.        ]),
 <a list of 30 Patch objects>)
Out[17]:
(array([  1.,   1.,   1.,   1.,   1.,   1.,   2.,   2.,   2.,   2.,   2.,
          2.,   2.,   3.,   3.,   3.,   3.,   3.,   6.,   7.,   9.,  10.,
         11.,  11.,  19.,  39.,  65., 127., 174., 197.]),
 array([0.        , 0.16666667, 0.33333333, 0.5       , 0.66666667,
        0.83333333, 1.        , 1.16666667, 1.33333333, 1.5       ,
        1.66666667, 1.83333333, 2.        , 2.16666667, 2.33333333,
        2.5       , 2.66666667, 2.83333333, 3.        , 3.16666667,
        3.33333333, 3.5       , 3.66666667, 3.83333333, 4.        ,
        4.16666667, 4.33333333, 4.5       , 4.66666667, 4.83333333,
        5.        ]),
 <a list of 30 Patch objects>)
Out[17]:
Text(0.5, 0, '评分')
In [18]:
#第五题
 #画成下方所示图例
 #数据预处理
d1 = merge_data["评价数"][merge_data["购买人数"] < 500]
d2 = merge_data["评价数"][merge_data["购买人数"] >= 500][merge_data["购买人数"] < 1000 ]
d3 = merge_data["评价数"][merge_data["购买人数"] >= 1000][merge_data["购买人数"] < 1500 ]
d4 = merge_data["评价数"][merge_data["购买人数"] >= 1500][merge_data["购买人数"] < 2000 ]
d5 = merge_data["评价数"][merge_data["购买人数"] >= 2000]
d = [d1, d2, d3, d4]#, d5]
 #绘图
fig=plt.figure(figsize=(10,8))
ax = fig.add_subplot(1,1,1)
ax.boxplot(d,
           patch_artist='Patch',
           labels=['<500','[500,1000)','[1000,1500)','[1500,2000)'],
           boxprops = {'color':'#2196F3','facecolor':'#BBDEFB'}
          )
ax.set_title("购买人数评价数目关系图")
ax.set_xlabel("购买人数")
ax.set_ylabel("评价数")
plt.show()
Out[18]:
{'whiskers': [<matplotlib.lines.Line2D at 0x232ac416d30>,
  <matplotlib.lines.Line2D at 0x232ac4250d0>,
  <matplotlib.lines.Line2D at 0x232ac42d400>,
  <matplotlib.lines.Line2D at 0x232ac42d760>,
  <matplotlib.lines.Line2D at 0x232ac437a90>,
  <matplotlib.lines.Line2D at 0x232ac437df0>,
  <matplotlib.lines.Line2D at 0x232ac44d160>,
  <matplotlib.lines.Line2D at 0x232ac44d4c0>],
 'caps': [<matplotlib.lines.Line2D at 0x232ac425430>,
  <matplotlib.lines.Line2D at 0x232ac425790>,
  <matplotlib.lines.Line2D at 0x232ac42dac0>,
  <matplotlib.lines.Line2D at 0x232ac42de20>,
  <matplotlib.lines.Line2D at 0x232ac441190>,
  <matplotlib.lines.Line2D at 0x232ac4414f0>,
  <matplotlib.lines.Line2D at 0x232ac44d820>,
  <matplotlib.lines.Line2D at 0x232ac44db80>],
 'boxes': [<matplotlib.patches.PathPatch at 0x232ac4169a0>,
  <matplotlib.patches.PathPatch at 0x232ac42d040>,
  <matplotlib.patches.PathPatch at 0x232ac4376d0>,
  <matplotlib.patches.PathPatch at 0x232ac441d60>],
 'medians': [<matplotlib.lines.Line2D at 0x232ac425af0>,
  <matplotlib.lines.Line2D at 0x232ac4371c0>,
  <matplotlib.lines.Line2D at 0x232ac441850>,
  <matplotlib.lines.Line2D at 0x232ac44dee0>],
 'fliers': [<matplotlib.lines.Line2D at 0x232ac425df0>,
  <matplotlib.lines.Line2D at 0x232ac4374c0>,
  <matplotlib.lines.Line2D at 0x232ac441b50>,
  <matplotlib.lines.Line2D at 0x232ac456220>],
 'means': []}
Out[18]:
Text(0.5, 1.0, '购买人数评价数目关系图')
Out[18]:
Text(0.5, 0, '购买人数')
Out[18]:
Text(0, 0.5, '评价数')
In [ ]: